package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.InspectionRuleDTO;
import com.neusoft.neuiotms.dcom.model.InspectionRule;
import com.neusoft.neuiotms.dcom.model.InspectionRuleProject;
import com.neusoft.neuiotms.dcom.model.enums.InspectionRuleStatus;
import com.neusoft.neuiotms.dcom.repository.InspectionRuleProjectRepository;
import com.neusoft.neuiotms.dcom.repository.InspectionRuleRepository;

@Component
public class InspectionRuleConverter extends AbstractConverter<InspectionRule, InspectionRuleDTO> {

    @Autowired
    private InspectionRuleRepository inspectionRuleRepository;

    @Autowired
    private BaseIdGenerator inspectionRuleCodeGenerator;

    @Autowired
    private InspectionRuleProjectConverter ruleProjectConverter;

    @Autowired
    private InspectionRuleProjectRepository projectRepository;

    @Override
    public InspectionRule toModel(final InspectionRuleDTO dto) {
        InspectionRule model;
        if (dto.isNew()) {
            model = new InspectionRule();
        } else {
            model = this.inspectionRuleRepository.getOne(dto.getId());
        }
        model.setCode(
                StringUtils.isEmpty(dto.getCode()) ? this.inspectionRuleCodeGenerator.generateCode() : dto.getCode());
        model.setName(dto.getName());
        model.setCronTime(dto.getCronTime());
        model.setDuration(dto.getDuration());
        model.setEndDateTime(dto.getEndDateTime());
        model.setStartDateTime(dto.getStartDateTime());
        model.setInspectionType(dto.getInspectionType());
        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());
        model.setStatus(InspectionRuleStatus.valueOf(dto.getStatus()));
        model.setUserCode(dto.getUserCode());
        model.setUserName(dto.getUserName());
        model.setVersion(dto.getVersion());

        final List<InspectionRuleProject> projects = model.getInspectionProject().stream().collect(Collectors.toList());
        projects.forEach(t -> {
            model.getInspectionProject().remove(t);
            this.projectRepository.delete(t);
        });
        if ((dto.getInspectionProject() != null) && !dto.getInspectionProject().isEmpty()) {
            dto.getInspectionProject().stream().map(t -> {
                final InspectionRuleProject p = this.ruleProjectConverter.toModel(t);
                p.setInspectionRule(model);
                return p;
            }).forEach(model.getInspectionProject()::add);
        }

        return model;
    }

    @Override
    public InspectionRuleDTO toDTO(final InspectionRule model, final boolean forListView) {
        final InspectionRuleDTO dto = new InspectionRuleDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setCreateTime(model.getCreateTime());
        dto.setCronTime(model.getCronTime());
        dto.setDuration(model.getDuration());
        dto.setEndDateTime(model.getEndDateTime());
        dto.setInspectionType(model.getInspectionType());
        dto.setName(model.getName());
        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());
        dto.setStartDateTime(model.getStartDateTime());
        dto.setStatus(model.getStatus().toString());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setUserCode(model.getUserCode());
        dto.setUserName(model.getUserName());
        dto.setVersion(model.getVersion());
        if (!forListView) {
            dto.setInspectionProject(this.ruleProjectConverter.toListDTO(model.getInspectionProject()));
        }

        return dto;
    }

}
